package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.AccountAdapter;
import com.sxm.yiwei.adapters.AccountStreamAdapter;
import com.sxm.yiwei.beans.Stream;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class MyAccountStream extends BaseActivity implements View.OnClickListener{
    private String TAG = "stream";
    private ImageView back;
    private PullToRefreshListView list;
    private ImageView nodata;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private int page =1;
    private AccountStreamAdapter adapter;
    private JSONArray array;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_account_stream);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "");
        userId = sharedPreferences.getString("userId", "");
        token = sharedPreferences.getString("token", "");
        initStreamView();
        getStreamData(page);

    }
        //获取流水信息
    private void getStreamData(int page){
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETSTREAMLIST);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();
        object.put("pageNo",page+"");
        object.put("pageSize","20");
        params.addBodyParameter("params",object.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject obj = JSON.parseObject(result);
                String status = obj.getString("status");
                if (status.equals("1")){
                    ToList(result);
                }else {

                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d(TAG, "onError: 流水--"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
        //listvew添加数据
    private void ToList(final String data) {
        JSONObject obj = JSON.parseObject(data);
        array = obj.getJSONArray("data");
        final List<Stream.DataBean> streamList = new ArrayList<>();
        if (array.size()==0){

        }else {
            nodata.setVisibility(View.GONE);
        }

        for (int i = 0; i < array.size(); i++) {
            JSONObject obj2 = JSON.parseObject(String.valueOf(array.get(i)));
            Stream.DataBean dataBean = new Stream.DataBean();
            String amount = obj2.getString("amount");
            String createTime = obj2.getString("createTime");
            String isInflow = obj2.getString("isInflow");
            String orderCode = obj2.getString("orderCode");
            String title = obj2.getString("title");
            dataBean.setAmount(Double.valueOf(amount));
            dataBean.setCreateTime(Long.valueOf(createTime));
            dataBean.setOrderCode(orderCode);
            dataBean.setTitle(title);
            if (isInflow.equals("true")){
                dataBean.setIsInflow(true);
            }else {
                dataBean.setIsInflow(false);
            }
            streamList.add(dataBean);
        }
        adapter = new AccountStreamAdapter(MyAccountStream.this,streamList);
        list.setAdapter(adapter);

        list.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                new FinishRefresh().execute();
            }
        });

    }

    private void initStreamView() {
        backmain = (ImageView) findViewById(R.id.mystream_backmain);
        back = (ImageView) findViewById(R.id.accountstream_back);
        list = (PullToRefreshListView) findViewById(R.id.accountstream_list);
        nodata = (ImageView) findViewById(R.id.accountstream_nodata);
        back.setOnClickListener(this);
        backmain.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.accountstream_back:
                finish();
                break;
            case R.id.mystream_backmain:
                startActivity(new Intent(MyAccountStream.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }
    }

    private class FinishRefresh extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
                page++;
                getStreamData(page);
            } catch (InterruptedException e) {

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            adapter.notifyDataSetChanged();
            list.onRefreshComplete();
        }
    }
}
